package com.itheima.web.manager;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.google.gson.annotations.JsonAdapter;
import com.itheima.domain.db.Admin;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.web.exception.BusinessException;
import com.itheima.web.interceptor.AdminHolder;
import com.itheimma.service.db.AdminService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Component
public class AdminManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private AdminService adminService;

    public LineCaptcha verification(String uuid) {
        //新建一个lineCaptcha，验证码类。hutool生车验证码
        LineCaptcha lineCaptcha = new LineCaptcha(300, 100);
        //往redis中存储
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE+uuid, lineCaptcha.getCode(), Duration.ofMinutes(5));
        //返回验证码对象
        return lineCaptcha;
    }


    //制作token
    public ResponseEntity login(String username, String password, String verificationCode, String uuid) {
        //1、查找验证码
        String code = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);

        //2、验证验证码
        if (!StrUtil.equals(code, verificationCode)) {
            throw new BusinessException("验证码错误");
        }

        //检查一下用户信息是否存在
        Admin admin = adminService.findByUsername(username);

        //检查该对象是否存在
        if (ObjectUtil.isEmpty(admin)) {
            throw new BusinessException("用户不存在");
        }
        //检查密码
        password = SecureUtil.md5(password);
        if (! StrUtil.equals(admin.getPassword(), password)) {
            throw new BusinessException("密码不正确");
        }

        //3、验证完毕，销毁验证码
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE+uuid);

        //4、制作token
        admin.setPassword(null);
        Map<String, Object> map = BeanUtil.beanToMap(admin);
        String token = JwtUtil.createToken(map);

        //5、存进redis中
        String json = JSON.toJSONString(admin);
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN+token, json,Duration.ofHours(1));

        //6、返回token，返回的是map集合，前端要的是json，所以不是实体类就是map集合，string类型不是json格式
        HashMap<String, String> result = new HashMap<>();
        result.put("token", token);
        return ResponseEntity.ok(result);
    }


    //登出，把token删除
    public void logout(String token) {
        token = token.replaceAll("Bearer ", ConstantUtil.ADMIN_TOKEN);
        stringRedisTemplate.delete(token);
    }



}
